package com.woniu.park.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.park.controller.form.ParkspaceForm;
import com.woniu.park.entity.Parkspace;
import com.woniu.park.entity.User;
import com.woniu.park.service.IParkspaceService;
import com.woniu.park.util.Result;
import com.woniu.park.util.Status;
import lombok.extern.slf4j.Slf4j;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxb
 * @since 2020-09-05
 */
@RestController
@Slf4j
public class ParkspaceController {

    @Autowired
    private IParkspaceService parkspaceService;

    //新增车位信息
    @PostMapping("insertPark")
//    @RequiresPermissions("上架车位")
    public Result insertPark(@RequestBody ParkspaceForm[] parkspaceForms, HttpSession session){
        log.info("前端传入ParkspaceForm[]：{}",parkspaceForms);
        User user = (User)session.getAttribute("user");
        int userId = user.getUserId();

        for (ParkspaceForm parkspaceForm : parkspaceForms) {
            Parkspace parkspace = new Parkspace();
            BeanUtils.copyProperties(parkspaceForm,parkspace);
            parkspace.setParkspaceStatus(1);
            //session取userId  目前写死
            parkspace.setUserId(userId);
            parkspace.setParkspaceTime(new Date());
            parkspace.setParkspacePerm("未授权");
            parkspace.setParkspacePriceOther(parkspace.getParkspacePriceHour()*0.8);
            parkspace.setParkspaceLati(parkspaceForm.getParkspaceLati());
            parkspace.setParkspaceLong(parkspaceForm.getParkspaceLong());
            parkspaceService.insertPark(parkspace);
        }
        return Result.success();
    }

//    @RequiresPermissions("预约车位")
    @GetMapping("findPark")
    public Result findPark(){
        System.out.println(1);
      List<Parkspace> parkspaces=  parkspaceService.findPark();
      return Result.success("1","执行成功",parkspaces);
    }

    /*
        查询附近空闲车位
     */
    @GetMapping("Park")
    public Result freeParking(){
        List<Parkspace> list = parkspaceService.freeParking();
        return  Result.success("1","附近空余车位",list);
    }

    /*
        查询车位信息
     */
    @GetMapping("query")
    public Result selecrParkspace(int parkspaceId){
        Parkspace parkspace = parkspaceService.selecrParkspace(parkspaceId);
        return  Result.success("1","车位信息查询成功",parkspace);
    }

    /*

     */
}

